package com.zm.demo.core;

import com.google.gson.Gson;
import org.junit.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import static java.lang.System.load;
import static java.lang.System.out;

/**
 * Created by Administrator on 2019/7/16.
 */
public class IntegerTest {


    public static void main(String[]args){

        //设置运行时参数：-Djava.lang.Integer.IntegerCache.high=1000
        Integer i10 = 121;//自动装箱,超过默认上限，但是小于设置的上限，创建新实例
        Integer i11 = 121;//自动装箱,超过默认上限，但是小于设置的上限，创建新实例
        out.println(i10 == i11);//true
        out.println(1 << 62);//true
        out.println(Long.toBinaryString(1 << 64));//true
        out.println(Integer.valueOf("001"));//true

//        Properties properties = new Properties();
//        properties.setProperty("java.lang.Integer.IntegerCache.high", "1000");
//        sun.misc.VM.saveAndRemoveProperties(properties);
//        out.println();
        Long l1 = 1L;
        Long l2 = 1L;
        out.println(l1 == l2);

        t2();

    }

    private static void t(){
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        for (int i = 0; i < stackTraceElements.length; i++) {
            out.println(stackTraceElements[i]);
        }
    }
    private static void t2(){
        t();
    }



    @Test
    public void test1(){
        List<Integer> integerList = new ArrayList<>(500);
        for (int i = 10000000; i < 10000500; i++) {
            integerList.add(Integer.valueOf(i));
        }

//        long cust = 0;
//        for (int j = 0; j < 1000; j++) {
//            long start = System.nanoTime();
//            for (int i = 0; i < 500; i++) {
//                int lastRepayGradeMonth = integerList.get(i) == null ? 0 : (integerList.get(i) % 10000) / 100;
//            }
//            cust += (System.nanoTime() - start);
//        }
//        out.println("integer: " + cust);

        long cust1=0;
        for (int j = 0; j < 1000; j++) {
            long start = System.nanoTime();
            for (int i = 0; i < 500; i++) {
                int lastRepayGradeMonth = integerList.get(i) == null ? 0 : Integer.valueOf(integerList.get(i).toString().substring(4, 6));
            }
            cust1 += (System.nanoTime() - start);
        }
        out.println("string: " + cust1);
    }

    @Test
    public void test2(){
        List<Integer> integerList = new ArrayList<>(500);
        for (int i = 10000000; i < 10000500; i++) {
            integerList.add(Integer.valueOf(i));
        }

        long cust = 0;
        long cust1=0;
        for (int i = 0; i < 100 ; i++) {
            Random r= new Random();
            int index = r.nextInt(500);

            long start = System.nanoTime();
            int lastRepayGradeMonth = integerList.get(index) == null ? 0 : (integerList.get(index) % 10000)/100;
            cust += (System.nanoTime() - start);
            out.println("integer: " + lastRepayGradeMonth + " - " +  cust);


            long start1 = System.nanoTime();
            int lastRepayGradeMonth1 = integerList.get(index) == null ? 0 : Integer.valueOf(integerList.get(index).toString().substring(4, 6));
            cust1 +=(System.nanoTime() - start1);
            out.println("string: " + lastRepayGradeMonth1 + " - " +  cust1);


            out.println("");
        }
    }

    @Test
    public void test3(){
        Map<String, Object> map = new Gson().fromJson("{\"calcFeeAfterDays\":\"10\"}", Map.class);
        Integer calcFeeAfterDays = Integer.parseInt(map.getOrDefault("calcFeeAfterDays", "0").toString()) ;
        out.println(calcFeeAfterDays);
    }

    @Test
    public void test4(){
        out.println(BigDecimal.valueOf(24D).divide(BigDecimal.valueOf(200), 0, RoundingMode.UP));
    }

    @Test
    public void testBytes(){
        String s = "";
        out.println(s.getBytes().length);
        s="D";
        out.println(s.getBytes().length);
        s="null";
        out.println(s.getBytes().length);
        s="啊";
        out.println(s.getBytes().length);
    }

    @Test
    public void testMod(){
        out.println(20210531 % 100);
        out.println(20210228 % 100);
        out.println(20210201 % 100);
    }
}
